package shiyan.Javawebshiyan.shiyan2.src;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import javax.servlet.ServletContext;
import java.io.IOException;

@WebServlet("/countDisplayServlet")
public class CountDisplayServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 取出 ServletContext 中存的次数
        int count = (int) getServletContext().getAttribute("count");
        // 显示到页面
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().println("<h1>网站被访问了 " + count + " 次!</h1>");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
}